package com.qianji.petmall.coupon.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qianji.common.to.MemberPrice;
import com.qianji.common.to.SkuRoundTo;
import com.qianji.common.utils.PageUtils;
import com.qianji.common.utils.Query;
import com.qianji.petmall.coupon.dao.SkuLadderDao;
import com.qianji.petmall.coupon.entity.MemberPriceEntity;
import com.qianji.petmall.coupon.entity.SkuFullReductionEntity;
import com.qianji.petmall.coupon.entity.SkuLadderEntity;
import com.qianji.petmall.coupon.service.MemberPriceService;
import com.qianji.petmall.coupon.service.SkuFullReductionService;
import com.qianji.petmall.coupon.service.SkuLadderService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service("skuLadderService")
public class SkuLadderServiceImpl extends ServiceImpl<SkuLadderDao, SkuLadderEntity> implements SkuLadderService {

    @Autowired
    private SkuFullReductionService skuFullReductionService;
    @Autowired
    private MemberPriceService memberPriceService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuLadderEntity> page = this.page(
                new Query<SkuLadderEntity>().getPage(params),
                new QueryWrapper<SkuLadderEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveSkuInfo(SkuRoundTo skuRoundTo) {
        /**
         * //sms_sku_ladder  SMS
         *          private int fullCount;//满几件
         *          private BigDecimal discount;//打几折
         *          private int countStatus;//
         */
        SkuLadderEntity skuLadderEntity = new SkuLadderEntity();
        BeanUtils.copyProperties(skuRoundTo,skuLadderEntity);
        skuLadderEntity.setAddOther(skuRoundTo.getCountStatus());
        if (skuLadderEntity.getFullCount() > 0){
            this.save(skuLadderEntity);
        }
        /**
         * //sms_sku_full_reduction SMS
         *             private BigDecimal fullPrice;//满多少
         *             private BigDecimal reducePrice;//减多少
         *             private int priceStatus;
         */
        SkuFullReductionEntity skuFullReductionEntity = new SkuFullReductionEntity();
        BeanUtils.copyProperties(skuRoundTo,skuFullReductionEntity);
        skuFullReductionEntity.setAddOther(skuRoundTo.getPriceStatus());
        if (skuFullReductionEntity.getFullPrice().doubleValue()>0){
            skuFullReductionService.save(skuFullReductionEntity);
        }
        /**
         *  //sms_member_price
         *          *     private List<MemberPrice> memberPrice;
         */
        List<MemberPrice> memberPrice = skuRoundTo.getMemberPrice();
        if (memberPrice!=null) {
            List<MemberPriceEntity> collect = memberPrice.stream().map(item -> {
                MemberPriceEntity memberPriceEntity = new MemberPriceEntity();
                memberPriceEntity.setSkuId(skuRoundTo.getSkuId());
                memberPriceEntity.setMemberLevelId(item.getId());
                memberPriceEntity.setMemberLevelName(item.getName());
                memberPriceEntity.setMemberPrice(item.getPrice());
                memberPriceEntity.setAddOther(1);
                return memberPriceEntity;
            }).filter(entity->{
                return entity.getMemberPrice().doubleValue()>0;
            }).collect(Collectors.toList());
            memberPriceService.saveBatch(collect);
        }

    }

}